<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.batchId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="食堂id" prop="canteenId">
          <el-input v-model="dataForm.canteenId" placeholder="食堂id"></el-input>
      </el-form-item>
            <el-form-item label="食堂主厨id" prop="chedId">
          <el-input v-model="dataForm.chedId" placeholder="食堂主厨id"></el-input>
      </el-form-item>
          <el-form-item label="状态" prop="type">
          <el-input v-model="dataForm.type" placeholder="状态"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
          <el-form-item label="食堂管理员ID" prop="canteenAdminId">
          <el-input v-model="dataForm.canteenAdminId" placeholder="食堂管理员ID"></el-input>
      </el-form-item>
          <el-form-item label="审核时间" prop="auditTime">
          <el-input v-model="dataForm.auditTime" placeholder="审核时间"></el-input>
      </el-form-item>
          <el-form-item label="审核意见" prop="auditOpinion">
          <el-input v-model="dataForm.auditOpinion" placeholder="审核意见"></el-input>
      </el-form-item>
          <el-form-item label="供应商id" prop="supplierId">
          <el-input v-model="dataForm.supplierId" placeholder="供应商id"></el-input>
      </el-form-item>
          <el-form-item label="报价完成时间" prop="offerTime">
          <el-input v-model="dataForm.offerTime" placeholder="报价完成时间"></el-input>
      </el-form-item>
          <el-form-item label="供应商备注" prop="supplierRemake">
          <el-input v-model="dataForm.supplierRemake" placeholder="供应商备注"></el-input>
      </el-form-item>
          <el-form-item label="仓储员id" prop="storehouseClerkId">
          <el-input v-model="dataForm.storehouseClerkId" placeholder="仓储员id"></el-input>
      </el-form-item>
          <el-form-item label="收货拍照时间" prop="receivingGoodsTime">
          <el-input v-model="dataForm.receivingGoodsTime" placeholder="收货拍照时间"></el-input>
      </el-form-item>
          <el-form-item label="仓储员备注" prop="storehouseClerkRemake">
          <el-input v-model="dataForm.storehouseClerkRemake" placeholder="仓储员备注"></el-input>
      </el-form-item>
          <el-form-item label="确认到货时间" prop="confirmDatetime">
          <el-input v-model="dataForm.confirmDatetime" placeholder="确认到货时间"></el-input>
      </el-form-item>
          <el-form-item label="食堂主厨备注" prop="chedRemake">
          <el-input v-model="dataForm.chedRemake" placeholder="食堂主厨备注"></el-input>
      </el-form-item>
          <el-form-item label="金额" prop="amountMoney">
          <el-input v-model="dataForm.amountMoney" placeholder="金额"></el-input>
      </el-form-item>
          <el-form-item label="金额照片" prop="amountMoneyPhoto">
          <el-input v-model="dataForm.amountMoneyPhoto" placeholder="金额照片"></el-input>
      </el-form-item>
          <el-form-item label="是否付款" prop="isPayment">
          <el-input v-model="dataForm.isPayment" placeholder="是否付款"></el-input>
      </el-form-item>
          <el-form-item label="财务记录id" prop="financeId">
          <el-input v-model="dataForm.financeId" placeholder="财务记录id"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        batchId: '',
        canteenId: '',
        createDate: '',
        chedId: '',
        type: '',
        remark: '',
        canteenAdminId: '',
        auditTime: '',
        auditOpinion: '',
        supplierId: '',
        offerTime: '',
        supplierRemake: '',
        storehouseClerkId: '',
        receivingGoodsTime: '',
        storehouseClerkRemake: '',
        confirmDatetime: '',
        chedRemake: '',
        amountMoney: '',
        amountMoneyPhoto: '',
        isPayment: '',
        financeId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        canteenId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        chedId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        canteenAdminId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        auditTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        auditOpinion: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        supplierId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        offerTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        supplierRemake: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        storehouseClerkId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        receivingGoodsTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        storehouseClerkRemake: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        confirmDatetime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        chedRemake: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        amountMoney: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        amountMoneyPhoto: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isPayment: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        financeId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.batchId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/canteens/batch/${this.dataForm.batchId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.batchId ? 'post' : 'put']('/canteens/batch/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
